﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Events.Controls
{
    public class EventUsersControl : UserControl
    {

        public int EventID { get; set; }
        
        protected Repeater rpt;
        protected Literal ErrorLabel;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
        }

        void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem == null)
            {
                return;
            }
            var UserName = (HtmlTableCell)e.Item.FindControl("userName");
            var UserEmail = (HtmlTableCell)e.Item.FindControl("userEmail");
            var UserFirstName = (HtmlTableCell)e.Item.FindControl("UserFirstName");
            var UserLastName = (HtmlTableCell)e.Item.FindControl("UserLastName");
            var UserCompany = (HtmlTableCell)e.Item.FindControl("UserCompany");

            var profile = e.Item.DataItem as AttendeeDto;
            if (profile == null)
            {
                ErrorLabel.Text = Resources.Events.UserProfileIsNull;
                return;
            }
            UserName.InnerText = profile.Attendee.UserName;
            UserEmail.InnerText = profile.Attendee.Email;
            UserFirstName.InnerText = profile.Attendee.FirstName;
            UserLastName.InnerText = profile.Attendee.LastName;
            if (string.IsNullOrEmpty(profile.Attendee.Company))
            {
                UserCompany.InnerHtml = "&nbsp;";
            }
            else
            {
                UserCompany.InnerText = profile.Attendee.Company;
            }
            
        }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (EventID == 0)
                ErrorLabel.Text = "<p style='color:red'>"+Resources.Events.CannotFindEventId+"</p>";
                
            var repository = new EventRepository(SPContext.Current.Web);
            var users = repository.GetEventAttendees(EventID);
            if (users.Count != 0)
            {
                rpt.DataSource =users;
                rpt.DataBind();
            }
            else
            {
                ErrorLabel.Text = Resources.Events.NoRegisteredUsersForEvent;
            }
        }

        protected string GetEndDate(object dtoObject)
        {
            var dto = dtoObject as EventDto;
            if (dto == null)
                return string.Empty;

            //if (dto.StartTime.Date == dto.EndTime.Date)
            //    return string.Empty;

            return "- " + dto.EndTime.ToShortTimeString();
        }

        protected object Iif(bool condition, object trueValue, object falseValue)
        {
            return (condition ? trueValue : falseValue);
        }
    }
}